package com.shiro.demo.framework.shiro;

import com.shiro.demo.framework.captcha.CaptchaAuthFilter;
import com.shiro.demo.framework.shiro.AjaxFormAuthenticationFilter;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.AbstractShiroWebFilterConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroWebFilterConfig extends AbstractShiroWebFilterConfiguration {

    @Value("${captcha.enabled}")
    private boolean captchaEnabled;
    @Value("${captcha.type}")
    private String    captchaType;

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean shiroFilterFactoryBean = super.shiroFilterFactoryBean();

        //自定义过滤器
        Map<String, Filter> filters = new LinkedHashMap<>();
        AjaxFormAuthenticationFilter formAuthenticationFilter = new AjaxFormAuthenticationFilter();
        filters.put("formAuthenticationFilter", formAuthenticationFilter);
        filters.put("captchaAuthFilter", new CaptchaAuthFilter(captchaEnabled, captchaType));
        shiroFilterFactoryBean.setFilters(filters);

        return shiroFilterFactoryBean;
    }
}
